//
//  CountDownButton.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/3/29.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "CountDownButton.h"
#import "UIButton+ButtonStyle.h"


@interface CountDownButton()

@property (nonatomic,assign) NSInteger  countDownSeconds;
@property (nonatomic,strong)  dispatch_source_t timer;
@end



@implementation CountDownButton

+ (instancetype)buttonWithTime:(NSInteger)seconds {
    CountDownButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        btn.countDownSeconds =  seconds;
        [btn phoneVerificationCodeStyle];
        [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [btn setTitle:@"获取验证码" forState:UIControlStateDisabled];
       
    }
    return btn;
}

+ (instancetype)buttonWithTime:(NSInteger)seconds title:(NSString *)title{
    CountDownButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        btn.countDownSeconds = seconds;
        //UIColor *color = [UIColor colorWithRGB:0x3165A5];
        [btn styleWithNormalColor:kBlueColor highlightColor:kBlue2Color];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitle:title forState:UIControlStateDisabled];
    }
    return btn;
}

- (void)dealloc{
    LogDealloc
}

- (BOOL)isBeginTime{
    if(self.timer){
        return YES;
    }else{
        return NO;
    }
}

- (void)stopTimer{
    if (self.timer) {
        dispatch_source_cancel(self.timer);
        self.enabled = YES;
        [self setTitle:[NSString stringWithFormat:@"重新获取"] forState:UIControlStateNormal];
    }
}

//- (NSInteger)showTime{
//    NSDate * currentDate = [NSDate date];
//    AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    NSTimeInterval start = [appdelegate.backgroundDate timeIntervalSince1970]*1;
//    NSTimeInterval end = [currentDate timeIntervalSince1970]*1;
//    NSTimeInterval diffTime = end - start;
//    return diffTime;
//}


//- (void)beginTimeWithCompletion:(CountDownCompletion)completion{
//    __weak typeof(self) weakSelf = self;
//    // 剩余的时间（必须用__block修饰，以便在block中使用）
//   
//    __block NSInteger remainTime = self.countDownSeconds;
//    [self subscribe:EnterBackGroud_Notification handler:^(GLEvent *event) {
//        remainTime = remainTime - [self showTime];
//    }];
//    // 获取全局队列
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//    self.timer = timer;
//    // 每隔1s钟执行一次
//    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
//    // 在queue中执行event_handler事件
//    dispatch_source_set_event_handler(timer, ^{
//        if (remainTime <= 0) { // 倒计时结束
//            dispatch_source_cancel(timer);
//            // 回到主线程
//            dispatch_async(dispatch_get_main_queue(), ^{
//                weakSelf.enabled = YES;
//                [weakSelf setTitle:[NSString stringWithFormat:@"重新获取"] forState:UIControlStateNormal];
//                if (completion) {
//                     completion(weakSelf);
//                }
//            });
//        } else {
//            CDebugLog(@"remainTime %ld",(long)remainTime);
//            NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)remainTime];
//            // 回到主线程更新UI
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [weakSelf setTitle:[NSString stringWithFormat:@"%@秒后重新获取",timeStr] forState:UIControlStateDisabled];
//                weakSelf.enabled = NO;
//            });
//            remainTime--;
//        }
//    });
//    dispatch_resume(timer);
//}
//
//

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
